package com.letoken.platform.app.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.app.service.AppConfigService;
import com.letoken.platform.pub.constant.RedisKeyConstant;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.repo.admin.dao.AppUserSwapRecordMapper;
import com.letoken.platform.repo.admin.dao.AdminTokenMapper;
import com.letoken.platform.repo.admin.po.AdminIssueConfig;
import com.letoken.platform.repo.app.dao.AppFundV2InfoMapper;
import com.letoken.platform.repo.app.dao.AppUserIssueMapper;
import com.letoken.platform.repo.admin.manager.AdminIssueConfigManager;
import com.letoken.platform.repo.admin.manager.AdminSwapConfigManager;
import com.letoken.platform.repo.app.po.AppFundV2Info;
import com.letoken.platform.repo.app.req.config.QueryHistoryReq;
import com.letoken.platform.repo.app.res.config.*;
import com.letoken.platform.repo.utlis.PageDataUtil;
import com.letoken.platform.repo.utlis.WebUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @author letokenlabs
 * @date 2022/11/25$
 */
@Service
@Log4j2
public class AppConfigServiceImpl implements AppConfigService {


    @Autowired
    private AdminSwapConfigManager adminSwapConfigManager;

    @Autowired
    private AdminIssueConfigManager adminIssueConfigManager;

    @Autowired
    private AppUserSwapRecordMapper swapRecordMapper;

    @Autowired
    private AdminTokenMapper adminTokenMapper;

    @Autowired
    private AppUserIssueMapper appUserIssueMapper;

    @Resource
    private AppFundV2InfoMapper appFundV2InfoMapper;


    @Override
    public List<QuerySwapConfigRes> querySwapConfig() {
        return adminSwapConfigManager.querySwapConfig();
    }

    @Override
    public QueryMintConfigRes queryMintConfig() {
        AdminIssueConfig mintConfig = adminIssueConfigManager.queryLet();
        return null;
    }

    /**
     * 查询闪兑记录
     *
     * @return
     */
    @Override
    public PageData<QueryHistoryRes> queryQuickSwapHistory(QueryHistoryReq req) {
        Page<QueryHistoryRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Integer userId = WebUtils.getUserId();
        Page<QueryHistoryRes> res = swapRecordMapper.queryQuickSwapHistory(page, req, userId);
        return PageDataUtil.getPageData(res);
    }

    /**
     * 查询币种（下拉框）
     *
     * @return
     */
    @Override
    public List<QueryQuickSwapRes> queryQuickSwapTokenList() {
        return adminTokenMapper.queryQuickSwapTokenList();
    }

    @Cacheable(cacheNames = RedisKeyConstant.ISSUE_CONFIG, key = "'issueConfig'", sync = true)
    @Override
    public QueryAppIssueConfigRes queryLetokenIssueConfig() {
        return adminIssueConfigManager.queryLetokenIssueConfig();
    }

    @Override
    public BigDecimal queryLetokenCirculation() {
        Integer userId = WebUtils.getUserId();
        AppFundV2Info appFundV2Info = appFundV2InfoMapper.selectBYUserId(userId);
        if(null == appFundV2Info || null == appFundV2Info.getRetained()){
            return BigDecimal.ZERO;
        }
        return appFundV2Info.getRetained();
//        BigDecimal letokenCirculation = appUserIssueMapper.queryLetokenCirculation(userId);
//        if (letokenCirculation == null) {
//            return BigDecimal.ZERO;
//        }
//        return letokenCirculation;
    }


    @Override
    public BigDecimal coinageIssuancePendingCirculation() {
        Integer userId = WebUtils.getUserId();
        BigDecimal letokenCirculation = appUserIssueMapper.queryLetokenCirculation(userId);
        return letokenCirculation;
    }

}
